#!/bin/bash -xe

kata_coco_top_dir="$HOME/workspace/CoCo"

trustee_repo_url="https://gitee.com/hanliyang-kata-coco/trustee.git"
trustee_repo_branch="0.11.0-hygon"
trustee_repo_dir="${kata_coco_top_dir}/trustee"

coco_trustee_version="v0.11.0"
coco_trustee_tag="trustee${coco_trustee_version}"

if [ ! -e ${trustee_repo_dir} ]; then
  git clone ${trustee_repo_url} \
    -b ${trustee_repo_branch} \
    ${trustee_repo_dir}
fi

build_coco_rvps_docker_image () {
  echo "Build RVPS docker image"
  pushd ${trustee_repo_dir}

  sudo docker build \
    --build-arg HTTPS_PROXY=${https_proxy} \
    --build-arg HTTP_PROXY=${http_proxy} \
    -t rvps:${coco_trustee_tag} \
    -f rvps/docker/Dockerfile \
    .

  popd # ${trustee_repo_dir}

  docker image save \
    -o ${kata_coco_top_dir}/rvps_${coco_trustee_tag}.tar \
    rvps:${coco_trustee_tag}
}

build_coco_rvps_docker_image

build_coco_as_docker_image () {
  echo "Build AS docker image"
  pushd ${trustee_repo_dir}

  sudo docker build \
    --build-arg HTTPS_PROXY=${https_proxy} \
    --build-arg HTTP_PROXY=${http_proxy} \
    -t coco-as-grpc:${coco_trustee_tag} \
    -f attestation-service/docker/as-grpc/Dockerfile \
    --build-arg ATTESTER=all-attester \
    .

  popd # ${trustee_repo_dir}

  docker image save \
    -o ${kata_coco_top_dir}/coco-as-grpc_${coco_trustee_tag}.tar \
    coco-as-grpc:${coco_trustee_tag}
}

build_coco_as_docker_image

build_coco_kbs_docker_image () {
  echo "Build KBS docker image"
  pushd ${trustee_repo_dir}

  sudo DOCKER_BUILDKIT=1 docker build \
    --build-arg HTTPS_PROXY=${https_proxy} \
    --build-arg HTTP_PROXY=${http_proxy} \
    -t kbs-grpc-as:${coco_trustee_tag} \
    -f kbs/docker/coco-as-grpc/Dockerfile \
    .

  popd # ${trustee_repo_dir}

  docker image save \
    -o ${kata_coco_top_dir}/kbs-grpc-as_${coco_trustee_tag}.tar \
    kbs-grpc-as:${coco_trustee_tag}
}

build_coco_kbs_docker_image
